﻿using System.Linq;
using System.Linq.Expressions;

namespace AntsCode.StorageClientExtensions.QueryHandlers
{
    internal abstract class QueryHandler
    {
        public QueryHandler(object data, ContinuationToken continuationToken)
        {
            this.Data = data;
            this.ContinuationToken = continuationToken;
        }

        public object Data { get; protected set; }

        protected T GetExprFromUnary<T>(Expression expr) where T : Expression
        {
            UnaryExpression outerUe = (UnaryExpression)expr;
            return (T)outerUe.Operand;
        }

        protected T GetValueFromConstant<T>(Expression expr)
        {
            T value = default(T);

            if (expr.GetType() == typeof(ConstantExpression))
            {
                ConstantExpression ce = (ConstantExpression)expr;
                value = (T)ce.Value;
            }

            return value;
        }

        public ContinuationToken ContinuationToken
        {
            get;
            protected set;
        }
    }
}